import { StorageService } from './../../providers/StorageService';
import { Component, Injector, ChangeDetectorRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BasePage } from "../basePage";
// import { UserService } from '../../providers/UserService';
import { UserService } from '../../providers/UserService';
import { PIC_URL } from "./../../providers/Config";

import { CallNumber } from '@ionic-native/call-number';

/**
 * Generated class for the NaiShopDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-nai-shop-detail',
  templateUrl: 'nai-shop-detail.html',
})
export class NaiShopDetailPage extends BasePage {
  picture: string;
  service_id: any;
  isconcernBtn: any;
  teachUndata: boolean;
  nailUndata: boolean;

  /*用于保存用户的信息*/
  userinfo:any;
  // userService: any;
  viewCtrl: any;
  modalCtrl: any;
  contentArea: string = 'ShopShowPage';
  // func = "recommend";

  /**店铺秀 美甲师tab */
  isShopShow: boolean;
  isNailTeach: boolean;

  /**关注 */
  attentionNum: boolean;
  attentionVo = {
    focused_id: null,
    user_id: null
  }

  /**美甲店主页详情参数 */
  getShopNailListAPIVo = {
    shop_id: null,
    user_id: null,
  }
  shopInfo = [];
  showShopList: any;
  showNailList: any;
  tabnavParams: any;
  artist_level=0;
  Mdstar = [];
  Gray =[];
  constructor(injector: Injector,
      private callNumber: CallNumber,
      params: NavParams, 
      public userService: UserService, 
      public stc : StorageService,
      public cd: ChangeDetectorRef) {
    super(injector);
    //从美甲店传过来的
    // this.attentionVo.user_id = this.getShopNailListAPIVo.user_id = params.get('user_id');
    // this.getShopNailListAPIVo.shop_id = params.get('shop_id');
   
    //从搜索店铺传过来的
    let a=params.get('user_id2')
    
    console.log(a);
    if(a == undefined){
        this.userinfo = this.stc.read('userinfo');
        console.log(this.userinfo);
        if(this.userinfo != null) 
        this.attentionVo.user_id = this.getShopNailListAPIVo.user_id = this.userinfo.user_id;
        this.getShopNailListAPIVo.shop_id = params.get('shop_id');
    
    }else{
        this.attentionVo.user_id = this.getShopNailListAPIVo.user_id = params.get('user_id2');
        this.getShopNailListAPIVo.shop_id = params.get('shop_id2');
      
        alert(this.attentionVo.user_id+this.getShopNailListAPIVo.shop_id)
    }
    console.log(this.getShopNailListAPIVo);
    this.picture = PIC_URL;
  } 

  ionViewDidLoad() {
    console.log('ionViewDidLoad NaiShopDetailPage');
    this.isShopShow = true;
    this.getShopNailListAPI();
  }
  dismiss() {
    let data = { 'foo': 'bar' };
    this.viewCtrl.dismiss(data);
    // $ionicHistory.goBack();
  }
  
  /**调用手机拨号 */
  callNum(phone) {
    this.callNumber.callNumber(phone, true)
      .then(() => console.log('Launched dialer!'))
      .catch(() => console.log('Error launching dialer'));
  }

  /**选择可视模块 */
  selectFunc(funcName) {
    // this.contentArea = funcName;  //NailShopPage  美店
    if (funcName == 'ShopShowPage') {
      this.isNailTeach = false;
      this.isShopShow = true;
      console.log(this.showShopList)
      if (this.showShopList.code == 0) {
        let toast = this.toastCtrl.create({
          message: this.showShopList.msg,
          duration: 2000,
          position: 'top',
        })
        toast.present();
        this.teachUndata = true;
        return;
      }
      return;
    } else if (funcName == 'NailTeacherPage') {
      this.isShopShow = false;
      this.isNailTeach = true;
      console.log(this.showNailList)
      if (this.showNailList.code == 0) {
        let toast = this.toastCtrl.create({
          message: this.showNailList.msg,
          duration: 2000,
          position: 'top',
        })
        toast.present()
        this.nailUndata = true;
        return;
      }
      return;
    }

  }

  /**关注 */
  async unsetAttention(focusId) {
    if(this.userinfo==null){
      this.showAlert('您当前未登录，请登陆后操作！');
    }else{
      this.attentionVo.focused_id = focusId;
      console.log(this.attentionVo);
      let res = await this.userService.dofocusAPI(this.attentionVo);
      console.log(res);
      if (res.code == 1) {
        let toast = this.toastCtrl.create({
          message: res.msg,
          duration: 3000,
          position: 'top',
        })
        toast.present();
      }
      this.getShopNailListAPI();
    }
  }

  /**美甲店详情接口 */
  async getShopNailListAPI() {
    let res = await this.userService.doGetShopNailListAPI(this.getShopNailListAPIVo);
    console.log(res);
    this.shopInfo = [];
    this.showShopList = [];
    this.showNailList = [];
    this.shopInfo.push(res.shop_info);
    this.showShopList = res.show_shop_list;
    this.showNailList = res.show_nail_list;
    // console.log(this.showNailList[0].nails);
    this.artist_level = res.shop_info.shop_level-0;
   
      this.Mdstar=new Array(this.artist_level)
      this.Gray=new Array(5-this.artist_level)
  }

  /**跳转 到立即购买 */
  toShopInfo(id) {
    // this.navCtrl.push('PromptlyBuyPage');
    this.service_id = id;
    this.navCtrl.push("ShopInfoPage", { "id": this.service_id ,"shop_id":this.getShopNailListAPIVo.shop_id});
  }
  gotoNailDetail(i){
    this.navCtrl.push("NailerDetailPage", { "list":i});
  }
}
